//
//  RecruitmentCtrl.m
//  laowu
//
//  Created by Northwan on 15/5/8.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "RecruitmentCtrl.h"
#import "PublishDateCtrl.h"
#import "ChooseAddressCtrl.h"
#import "ChooseJobCtrl.h"
#import "PublishDateModel.h"
#import "ResumeListCtrl.h"
#import "ProjectListCtrl.h"

@interface RecruitmentCtrl ()<UISearchBarDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *positionLabel;
@property (weak, nonatomic) IBOutlet UILabel *publishTimeLabel;
@property (strong,nonatomic)PublishDateModel *dateMode;
@property (copy,nonatomic)NSString *city;
@property (strong,nonatomic)NSArray *jobArr;


- (IBAction)searchBtnAction:(id)sender;
- (IBAction)tapItemAction:(UIButton *)sender;

@property (nonatomic, assign) RecruitmentType type;

@end

@implementation RecruitmentCtrl

-(instancetype)initWithType:(RecruitmentType)type{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == RecruitmentTypeJobSearch) {
        [self setTitle:@"职位搜索"];
    }else{
        [self setTitle:@"求职信息搜索"];
    }
    
    [self.scrollView setAlwaysBounceVertical:YES];
    [self.scrollView setDelegate:self];
    [self.searchBar setDelegate:self];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(publishDateNortif:) name:nortif_date_choose_finish object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(chooseJobNortif:) name:nortif_job_choose_finish object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(chooseAddressNortif:) name:nortif_address_choose_finish object:nil];
}

-(void)chooseAddressNortif:(NSNotification *)nortif{
    _city = [nortif object];
    if(ISBLANK(_city)){
        NSString *city = [UserDefaultsHelper stringForKey:kLocationAddressCity];
        if(ISBLANK(city)){
            [_addressLabel setText:@"全国"];
        }else{
            [_addressLabel setText:city];
        }
    }else{
        [_addressLabel setText:_city];
    }
}

-(void)publishDateNortif:(NSNotification *)nortif{
    _dateMode =  [nortif object];
    if(ISBLANK([_dateMode dateText])){
        [_publishTimeLabel setText:@"所有日期"];
    }else{
        [_publishTimeLabel setText:[_dateMode dateText]];
    }
}

-(void)chooseJobNortif:(NSNotification*)nortif{
    _jobArr = [nortif object];
    NSMutableString *str = [[NSMutableString alloc]init];
    for (int i = 0; i<_jobArr.count; i++) {
        [str appendString:_jobArr[i]];
    }
    if(ISBLANK(str)){
       [_positionLabel setText:@"所有职能"];
    }else{
       [_positionLabel setText:str];
    }
}

- (IBAction)searchBtnAction:(id)sender {
    NSString *searchText = self.searchBar.text;
    [self.searchBar endEditing:YES];
    if (self.type == RecruitmentTypeJobSearch) {
        ProjectListCtrl *ctrl = [ProjectListCtrl initWithSearch:searchText city:_city job:_jobArr date:_dateMode];
        ctrl.hideHeader = YES;
        [self.navigationController pushViewController:ctrl animated:YES];
    }else{
        ResumeListCtrl *ctrl = [[ResumeListCtrl alloc] initWithSearch:searchText city:_city job:_jobArr date:_dateMode];
        ctrl.hideHeader = YES;
        [self.navigationController pushViewController:ctrl animated:YES];
    }
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *searchText = self.searchBar.text;
    [self.searchBar endEditing:YES];
    NSLog(@"键盘搜索:%@",searchText);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.searchBar endEditing:YES];
}

- (IBAction)tapItemAction:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            NSLog(@"选择地区");
            [self.navigationController pushViewController:[[ChooseAddressCtrl alloc]init] animated:YES];
            break;
        case 1:
            NSLog(@"选择岗位");
            [self.navigationController pushViewController:[[ChooseJobCtrl alloc]init] animated:YES];
            break;
        case 2:
            NSLog(@"选择发布日期");
            [self.navigationController pushViewController:[[PublishDateCtrl alloc]init] animated:YES];
            break;
        default:
            break;
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
